define(function (require, exports, modules) {
    //加载独立的模块
    let $ = require("jquery");
    require("jquery.validate");
    require("idcode");
    require("cookie");
    //登录方式的切换
    // //验证用户名
    $.validator.addMethod("checkName", function (val, ele) {
        return /^[1]\d{10}$/.test(val) || /^\w+@\w+\.\w+$/.test(val);
    })
    //验证验证码
    $.validator.addMethod("checkCode", function (val, ele) {
        return $.idcode.validateCode();
    })
    modules.exports = {
        init: function () {
            //加载验证码
            $.idcode.setCode({
                e: 'idcode',
                codeType: {name: 'follow', len: 6},
                codeTip: '换一张',
                inputID: 'aijquery_yzm'  //引用验证码输入框Id
            });
        },
        validate: function () {
            $("#form1").validate({
                messages: {
                    uname: {
                        required: "邮箱/手机号/会员卡号不能为空",
                        checkName: "邮箱/手机号/会员卡号错误，请重新输入",
                    },
                    upwd: {
                        required: "密码不能为空",
                    },
                },
                rules: {
                    uname: {
                        required: true,
                        checkName: true,
                    },
                    upwd: {
                        required: true,
                    },
                },
                submitHandler: function () {
                        $.ajax({
                            url: "../../server/user/login.php",
                            type: "post",
                            dataType:"json",
                            data: $("#form1").serialize(),
                        }).then(function (res) {
                            if (res.status == 2) {
                                sessionStorage.setItem("userInfo", JSON.stringify(res.data));
                                alert("登录成功");
                                let CookieList = JSON.parse($.cookie("carts") || '[]');
                                var arr=[];
                                CookieList.forEach((ele, index) =>{
                                    ele.u_id = res.data.u_id;
                                    ele.img = ele.img[0];
                                    console.log(ele);
                                    let p= $.ajax({
                                        url : "../../server/addCart.php",
                                        type : "post",
                                        data:ele
                                    });
                                    arr.push(p.promise());
                                })
                                Promise.all(arr).then(function(res){
                                    $.cookie("carts","",{expires:-1});
                                })
                                window.location = "index.html";
                            }else{
                                alert(res.msg);
                            }
                        })
                    return false;
                },
                errorPlacement: function (error, ele) {
                    error.appendTo(ele.parent("p"));
                    //存在一个bug.从验证正确再退回不正确状态,背景图片就不会显示了,考虑使用精灵图解决
                    ele.siblings("label").css("background", "url(./imgs/login/l_060.jpg) no-repeat 2px");
                },
                success: function (err, ele) {
                    $(ele).siblings("label").css("background", "");
                }
            })
        }
    }
    $(".title span").on("click", function (e) {
        $(e.target).addClass("current").siblings().removeClass("current");
        $("form").eq($(this).index()).addClass("current").siblings().removeClass("current");
    })
})